package com.hnkywl.sts.service.tjbb;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.tjbb.LrcsDao;
import com.hnkywl.sts.dao.tjbb.YjykhhzbDao;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-9-28
 * Time: 下午5:13
 * To change this template use File | Settings | File Templates.
 */
@Service
public class YjykhhzbService {
    private YjykhhzbDao yjykhhzbDao;
    private LrcsDao lrcsDao;

    public String yjykhhzList(Integer year, Integer month) {
        Map<String, List> result = yjykhhzbDao.yjykhhzList(year, month);
        List<Map> rows = result.get("rows");
        List<Map> footer = result.get("footer");
        Gson gson = new GsonBuilder().create();
        String rowsJson = gson.toJson(rows);
        String footerJson = gson.toJson(footer);
        String json = "{\"rows\":" + rowsJson + ",\"footer\":" + footerJson + "}";
        return json;
    }

    public String findLrcsList() {
        Page<Map> page = lrcsDao.findLrcsList();
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        String json = gson.toJson(page.getResult());
        return json;
    }

    @Transactional
    public void updateLrcs(Integer year, Integer month, Float ll) {
        int mons;
        if (month < 10) {
            mons = Integer.parseInt(String.valueOf(year) + ("0" + String.valueOf(month)));
        } else {
            mons = Integer.parseInt(String.valueOf(year) + String.valueOf(month));
        }
        Date lastUpdateDate = new Date();
        Long id = YgService.getCurrentYg().getId();
        this.lrcsDao.updateLrcs(mons, ll, lastUpdateDate, id);
    }

    public String sjsr(Integer gsid, Integer year, Integer month, Integer pageNo, Integer pageSize) {
        Map<String, Object> result = yjykhhzbDao.sjsrList(gsid, year, month, pageNo, pageSize);
        Integer total = (Integer) result.get("total");
        List<Map> rows = (List<Map>) result.get("rows");
        List<Map> footer = (List<Map>) result.get("footer");
        Gson gson = new GsonBuilder().create();
        String rowsJson = gson.toJson(rows);
        String footerJson = gson.toJson(footer);
        String json = "{\"rows\":" + rowsJson + ",\"footer\":" + footerJson + ",\"total\":" + total + "}";
        return json;
    }

    public String zjcb(Integer gsid, Integer year, Integer month) {
        Map<String, Object> result = yjykhhzbDao.zjcbList(gsid, year, month);
        List<Map> rows = (List<Map>) result.get("rows");
        List<Map> footer = (List<Map>) result.get("footer");
        Gson gson = new GsonBuilder().create();
        String rowsJson = gson.toJson(rows);
        String footerJson = gson.toJson(footer);
        String json = "{\"rows\":" + rowsJson + ",\"footer\":" + footerJson + "}";
        return json;
    }

    @Autowired
    public void setYjykhhzbDao(YjykhhzbDao yjykhhzbDao) {
        this.yjykhhzbDao = yjykhhzbDao;
    }

    @Autowired
    public void setLrcsDao(LrcsDao lrcsDao) {
        this.lrcsDao = lrcsDao;
    }
}
